package com.example.mymusicdisplay.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.data.music.RequestResult;
import com.example.mymusicdisplay.data.music.Songs;
import com.example.mymusicdisplay.databinding.FragmentRankingBinding;
import com.example.mymusicdisplay.utils.SearchUtil;
import com.example.mymusicdisplay.view.adapter.RankingAdapter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class RankingFragment extends Fragment {

    private FragmentRankingBinding mRankingBinding;
    private RankingAdapter mRankingAdapter;
    private List<Songs> rankingList = new ArrayList();

    public RankingFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mRankingBinding = DataBindingUtil.inflate(
            LayoutInflater.from(requireContext()), R.layout.fragment_ranking, container, false);
        ViewCompat.setOnApplyWindowInsetsListener(mRankingBinding.swipeLayout, new OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                layoutParams.topMargin = insets.getSystemWindowInsetTop();
                return insets;
            }
        });
        return mRankingBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        SearchUtil.getSearchResult("xu").subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<RequestResult>() {
                @Override
                public void accept(RequestResult requestResult) throws Throwable {
                    rankingList = requestResult.getResult().getSongs();
                    mRankingAdapter.setMusicDataList(rankingList);
                }
            });
        mRankingAdapter = new RankingAdapter(requireContext());
        mRankingBinding.recView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        mRankingBinding.recView.setAdapter(mRankingAdapter);

        mRankingBinding.swipeLayout.setColorSchemeResources(R.color.purple_200, R.color.black);
        mRankingBinding.swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                SearchUtil.getSearchResult("ku").subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<RequestResult>() {
                        @Override
                        public void accept(RequestResult requestResult) throws Throwable {
                            rankingList = requestResult.getResult().getSongs();
                            mRankingAdapter.setMusicDataList(rankingList);
                            mRankingBinding.swipeLayout.setRefreshing(false);
                        }
                    });
            }
        });
    }
}